/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package com.airlogger.log
{
	import flash.display.DisplayObjectContainer;
	
	import com.airlogger.event.AirLoggerLogEvent;
	import com.airlogger.log.control.AddLog;
	import com.airlogger.log.control.ClearLog;
	import com.airlogger.log.control.ConnectSWF;
	import com.airlogger.log.control.DisconnectSWF;
	import com.airlogger.log.control.FilterLog;
	import com.airlogger.log.control.SetFocus;
	import com.airlogger.log.event.LevelEvent;
	import com.airlogger.log.model.AirLoggerModelList;
	import com.airlogger.log.model.UIModel;
	import com.airlogger.log.view.AirLoggerViewList;
	import com.airlogger.log.view.LogView;
	import com.airlogger.proxy.AirLoggerProxyEventList;
	import com.airlogger.proxy.AirLoggerProxyListener;
	import com.bourre.log.LogLevel;
	import com.bourre.plugin.AbstractPlugin;
	import com.airlogger.event.SWFEvent;
	import com.airlogger.log.control.ChangeTabName;
	import com.bourre.log.PixlibDebug;	

	public class AirLoggerLogPlugin extends AbstractPlugin implements AirLoggerProxyListener
	{
		public static const onChangeFilterEVENT : String = "onFilterChange";		public static const onSWFDisconnectEVENT : String = "onSWFDisconnect";		public static const onSWFConnectEVENT : String = "onSWFConnect";
		
		public function AirLoggerLogPlugin( view : DisplayObjectContainer )
		{
			super();
			
			getController().pushCommandClass( AirLoggerProxyEventList.onAddLogEVENT, AddLog );
			getController().pushCommandClass( AirLoggerProxyEventList.onClearLogsEVENT, ClearLog );
			getController().pushCommandClass( AirLoggerProxyEventList.onFocusEVENT, SetFocus );
			getController().pushCommandClass( onSWFConnectEVENT, ConnectSWF );
			getController().pushCommandClass( onSWFDisconnectEVENT, DisconnectSWF );
			getController().pushCommandClass( AirLoggerProxyEventList.onTabNameChangedEVENT, ChangeTabName );
			getController().pushCommandClass( onChangeFilterEVENT, FilterLog );
			
			var uiModel : UIModel = new UIModel ( this, AirLoggerModelList.UI );
			
			var logView : LogView = new LogView ( this, AirLoggerViewList.LOG, view );
			
			uiModel.addListener( logView );
		}
		
		public function onConnection( event : SWFEvent ) : void
		{
			event.type = onSWFConnectEVENT;
			firePrivateEvent( event );
		}
		
		public function onDeconnection( event : SWFEvent):void
		{
			//event.type = onSWFDisconnectEVENT;
			//firePrivateEvent( event );
		}
		
		public function onAddLog(event:AirLoggerLogEvent):void
		{
			firePrivateEvent( event );
		}
		
		public function onClearLogs(event:AirLoggerLogEvent):void
		{
			firePrivateEvent( event );
		}
		
		public function onFocus(event:AirLoggerLogEvent):void
		{
			firePrivateEvent( event );
		}
		
		public function fireOnClearLogsEvent () : void
		{
			var uimodel : UIModel = getModelLocator().getModel( AirLoggerModelList.UI ) as UIModel;
			var e : AirLoggerLogEvent = new AirLoggerLogEvent ( AirLoggerProxyEventList.onClearLogsEVENT, this, uimodel.getFocus() );
			firePrivateEvent( e );
		}
		
		public function fireOnChangeFilterEvent ( level : LogLevel ) : void
		{
			var uimodel : UIModel = getModelLocator().getModel( AirLoggerModelList.UI ) as UIModel;
			var e : LevelEvent = new LevelEvent ( onChangeFilterEVENT, this, uimodel.getFocus(), level );
			firePrivateEvent( e );
		}
		
		public function fireOnDeconnectionEvent ( id : String ) : void
		{
			//PixlibDebug.DEBUG( "in fireOnDeconnectionEvent : " + id );
			
			var e : SWFEvent = new SWFEvent(AirLoggerLogPluginEventList.onDeconnectionEVENT, this, id );
			firePublicEvent( e );
		}
		
		public function onTabNameChanged(event : SWFEvent) : void
		{
			firePrivateEvent( event );
		}
	}
}